// 是否为本地时间的同一天?
export function isSameDay(d0: Date, d1: Date) {
    if (d0 == null || d1 == null) return false;
    return d0.getFullYear() == d1.getFullYear() && d0.getMonth() == d1.getMonth() && d0.getDate() == d1.getDate();
}

export type MSR = {
    sign: number,
    td: number,
    th: number,
    tm: number,
    ts: number,
    tms: number,
    h: number,
    m: number,
    s: number,
    ms: number,
};

export function MSResolve(tms): MSR {
    let sign = Math.sign(tms);
    tms = Math.abs(tms);
    let ms = tms % 1000;
    let ts = Math.floor(tms / 1000);
    let s = ts % 60;
    let tm = Math.floor(ts / 60);
    let m = tm % 60;
    let th = Math.floor(tm / 60);
    let h = th % 24;
    let td = Math.floor(th / 24);

    return {
        sign: sign,
        td: td,
        th: th,
        tm: tm,
        ts: ts,
        tms: tms,
        h: h,
        m: m,
        s: s,
        ms: ms
    }
};

export function MSRToString_th_m_s(msr: MSR) {
    return `${msr.sign < 0 ? '-' : ''}${msr.th.toString().padStart(2, '0')}:${msr.m.toString().padStart(2, '0')}:${msr.s.toString().padStart(2, '0')}`;
}

export function MSRToString_tm_s(msr: MSR) {
    return `${msr.sign < 0 ? '-' : ''}${msr.tm.toString().padStart(2, '0')}:${msr.s.toString().padStart(2, '0')}`;
}

export function MSRToString_tm0_s(msr: MSR) {
    return `${msr.sign < 0 ? '-' : ''}${msr.tm}:${msr.s.toString().padStart(2, '0')}`;
}

export function MSRToCH_tm_s(msr: MSR) {
    return `${msr.tm}m${msr.s > 0 ? msr.s + 's' : ''}`;
}

export function MSRToString_th_m(msr: MSR) {
    return `${msr.th > 0 ? (msr.th + 'h') : ''}` + `${msr.m > 0 ? (msr.m + 'm') : ''}`;
}

// 1H,30M
export function MSRToString_th_m_short(msr: MSR) {
    if (msr.th > 0) {
        if (msr.m == 0) {
            return `${msr.th}H`;
        } else {
            return `${msr.th}H${msr.m}M`;
        }
    } else {
        return `${msr.m}M`;
    }
}

// 1:59:59, 59:59
export function MSRToString_th_m_s_2(msr: MSR) {
    if (msr.th > 0) {
        return `${msr.sign < 0 ? '-' : ''}${msr.th.toString()}:${msr.m.toString().padStart(2, '0')}:${msr.s.toString().padStart(2, '0')}`;
    }
    return `${msr.sign < 0 ? '-' : ''}${msr.tm.toString()}:${msr.s.toString().padStart(2, '0')}`;
}

/**
 * 为每日刷新的逻辑计算距下次刷新的剩余时间（毫秒数）
 * @param curDate 当前时间
 * @param saveAt 记录点上次存档时间 
 * @param refreshAtList 一日中的刷新点列表 (要按顺序给，先早后晚。Date形式会忽略掉年月日，但是要1901年之后的，直接选用当天时间即可)
 * @returns {remain: 剩余毫秒数, next: 对应的下个刷新点Date} 通常，剩余毫秒数<=0就该刷新了，同时要更新存档时间到当前时刻
 */
export function DailyRefreshRemain(curDate: Date, saveAt: Date, refreshAtList: Date[]): { remain: number, next: Date } {
    // 处理时差，这样全球各个时区的玩家将会拥有各自不同的日刷新点
    refreshAtList = refreshAtList.map(_ => {
        const date = new Date(_);
        date.setUTCMinutes(date.getUTCMinutes() + curDate.getTimezoneOffset());
        return date;
    });
    if (saveAt == null) return { remain: 0, next: curDate }; // 特殊情况：当没有上次记录点刷新时间时

    // 检查上个存档点当天的所有刷新点
    for (let i = 0; i < refreshAtList.length; i++) {
        const todayRefreshPoint = new Date(refreshAtList[i]);
        todayRefreshPoint.setFullYear(saveAt.getFullYear(), saveAt.getMonth(), saveAt.getDate());
        if (saveAt.valueOf() < todayRefreshPoint.valueOf()) {
            return {
                remain: todayRefreshPoint.valueOf() - curDate.valueOf(),
                next: todayRefreshPoint
            };
        }
    }

    // 检查上个存档点后一天的第一个刷新点
    const tomorrowRefreshPoint = new Date(refreshAtList[0]);
    tomorrowRefreshPoint.setFullYear(saveAt.getFullYear(), saveAt.getMonth(), saveAt.getDate() + 1);
    return {
        remain: tomorrowRefreshPoint.valueOf() - curDate.valueOf(),
        next: tomorrowRefreshPoint
    };
}

export function dateToStr(nowDate: Date) {
    var now = nowDate;
    var year = now.getFullYear();
    var month = String(now.getMonth() + 1).padStart(2, '0');
    var day = String(now.getDate()).padStart(2, '0');
    var hours = String(now.getHours()).padStart(2, '0');
    var minutes = String(now.getMinutes()).padStart(2, '0');
    var seconds = String(now.getSeconds()).padStart(2, '0');
    return year + '/' + month + '/' + day + ' ' + hours + ':' + minutes + ':' + seconds;
}

export function dateTo_ymd(date: Date) {
    var year = date.getFullYear();
    var month = String(date.getMonth() + 1).padStart(2, '0');
    var day = String(date.getDate()).padStart(2, '0');
    return `${year}.${month}.${day}`;
}

export function dateTo_ymd2(date: Date) {
    var year = date.getFullYear();
    var month = String(date.getMonth() + 1).padStart(2, '0');
    var day = String(date.getDate()).padStart(2, '0');
    return `${year}/${month}/${day}`;
}

export function nowStamp() {
    return Math.round(Date.now() / 1000);
}